import time


def data_to_stamp(time_str_data, fromate="%Y-%m-%d %H:%M:%S"):
    '''
    将字符串型的时间类型转换成时间戳
    '''
    try:
        time_array = time.strptime(time_str_data, fromate)
        time_stamp = str(int(time.mktime(time_array)) * 1000)
    except:
        time_stamp = "trans error"
    return time_stamp


def stamp_to_date(time_stamp):
    '''
    将时间戳转换成字符串型的时间类型
    '''
    time_stamp = float(time_stamp) / 1000
    time_array = time.localtime(time_stamp)
    str_time = time.strftime("%Y-%m-%d %H:%M:%S", time_array)
    return str_time


def time_trans(get_time):
    '''
    将时间格式转换成另一种格式
    '''
    input_times = get_time.split("\n")
    res = ""
    for input_time in input_times:
        len_time = len(input_time)
        if len_time == 13:
            out_time = stamp_to_date(input_time)
        elif len_time == 10:
            out_time = stamp_to_date(input_time + "000")
        else:
            out_time = data_to_stamp(input_time)
        res = res + out_time + "\n"
    return res


def get_time(self):
    '''
    获取当前时间和时间戳
    '''
    time_now = time.time()
    stamp_now = str(int(round(time_now * 1000)))
    return {"date": stamp_to_date(stamp_now), "stamp": stamp_now}
